package com.wtgroup.demo.面试题.我的面试;

import com.wtgroup.demo.common.util.Mock;
import leetcodeauto.common.ListNode;

/**
 * 2023年6月27日 高度出租车
 *
 * 1-2-2-3-3-4-5
 * 1-4-5
 *
 * @see [82. 删除排序链表中的重复元素 II - 力扣（LeetCode）](https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/)
 * @author nisus
 * @version 0.1
 * @since 2023/6/27 21:18
 */
public class Q_有序列表删除重复节点 {
    public static void main(String[] args) {
        ListNode head = Mock.listNode(new int[]{1, 2, 2, 3, 3, 4, 5});
        ListNode ret = func(head);
        System.out.println(ret.prettyString());
    }

    // Fail
    static ListNode func(ListNode head) {
        if (head == null) {
            return null;
        }

        ListNode d = new ListNode(-1);
        d.next = head;
        ListNode curr = head;
        ListNode pre = d;
        while (curr.next != null && curr.next.val != curr.val) {
            pre = curr;
            curr = curr.next;
        }

        while (curr.next != null && curr.next.val == curr.val) {
            curr = curr.next;
        }

        pre.next = curr.next;

        if (pre.next == null) {
            return d.next;
        }

        func(pre);

        return d.next;
    }

}
